package com.broada.apm.model.performance;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class IndicatorItem {

	private static abstract class BaseIndicatorItem {
		protected String code;
		protected String name;

		protected BaseIndicatorItem(String code, String name) {
			this.code = code;
			this.name = name;
		}

		public String getCode() {
			return code;
		}

		public String getName() {
			return name;
		}
	}

	public static class Str extends BaseIndicatorItem {

		public Str(String code, String name) {
			super(code, name);
		}

		public String get(AbstractPerformance performance) {
			return (String) performance.get(code);
		}

		public void set(AbstractPerformance performance, String val) {
			performance.put(code, val);
		}
	}

	public static class Int extends BaseIndicatorItem {

		public Int(String code, String name) {
			super(code, name);
		}

		public int get(AbstractPerformance performance) {
			return performance.containsKey(code) ? ((Number) performance.get(code)).intValue() : 0;
		}

		public void set(AbstractPerformance performance, int val) {
			performance.put(code, val);
		}
	}

	public static class Long extends BaseIndicatorItem {

		public Long(String code, String name) {
			super(code, name);
		}

		public long get(AbstractPerformance performance) {
			return performance.containsKey(code) ? ((Number) performance.get(code)).longValue() : 0L;
		}

		public void set(AbstractPerformance performance, long val) {
			performance.put(code, val);
		}
		
		public void add(AbstractPerformance performance, java.lang.Long val) {
			set(performance, get(performance) + (val!=null?val:0));
		}
		
		public void max(AbstractPerformance performance, java.lang.Long val) {
			set(performance, Math.max(get(performance), (val!=null?val:0)));
		}
		
		public void min(AbstractPerformance performance, java.lang.Long val) {
			set(performance, Math.min(get(performance)==0?(val!=null?val:0):get(performance), (val!=null?val:0)));
		}
	}

	public static class Double extends BaseIndicatorItem {
		private static java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");

		public Double(String code, String name) {
			super(code, name);
		}

		public double get(AbstractPerformance performance) {
			return performance.containsKey(code) ? ((Number) performance.get(code)).doubleValue() : 0D;
		}

		public void set(AbstractPerformance performance, double val) {

			performance.put(code, java.lang.Double.parseDouble(df.format(val)));
		}
	}

	public static class Bool extends BaseIndicatorItem {

		public Bool(String code, String name) {
			super(code, name);
		}

		public boolean get(AbstractPerformance performance) {
			return performance.containsKey(code) ? (java.lang.Boolean) performance.get(code) : false;
		}

		public void set(AbstractPerformance performance, boolean val) {
			performance.put(code, val);
		}
	}

	public static class Date extends BaseIndicatorItem {
		private static final String YYYY_MM_DD_T_HH_MM_SS_ZZ = "yyyy-MM-dd'T'HH:mm:ssZZ";

		public Date(String code, String name) {
			super(code, name);
		}

		public java.util.Date get(AbstractPerformance performance) {

			try {
				Object obj = performance.get(code);
				if (obj instanceof Date)
					return (java.util.Date) obj;
				if (obj instanceof String)
					return (java.util.Date) new SimpleDateFormat(YYYY_MM_DD_T_HH_MM_SS_ZZ).parse((String) obj);
				return null;
			} catch (ParseException e) {
				return null;
			}

		}

		public void set(AbstractPerformance performance, java.util.Date val) {
			performance.put(code, val);
		}
	}

}
